package com.fifth.home.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.HeadNav;
import com.fifth.home.mapper.HeadNavMapper;
import com.fifth.home.pojo.vo.FuzzySearch;
import com.fifth.home.service.IHeadNavService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fifth.home.utils.WebUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.netty.util.internal.StringUtil;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.annotation.WebFilter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-01
 */
@Service
public class HeadNavServiceImpl extends ServiceImpl<HeadNavMapper, HeadNav> implements IHeadNavService {
    @Resource
    private HeadNavMapper headNavMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 头部导航  模糊查询
     *
     * @param fuzzySearch
     * @return
     */
    @Override
    public CommonResult fuzzyQuery(FuzzySearch fuzzySearch, Integer currentPage, Integer pageSize) {
        IPage<HeadNav> page = new Page<>(currentPage, pageSize);
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank( fuzzySearch.getHeadName()),"head_name", fuzzySearch.getHeadName());
        wrapper.eq(StringUtils.isNotBlank(fuzzySearch.getIsDisable()), "is_disable", fuzzySearch.getIsDisable());
        wrapper.eq("is_deleted", "0");
        wrapper.eq("is_add","0");
        wrapper.orderByAsc("head_id");

        IPage<HeadNav> headNavIPage = headNavMapper.selectPage(page, wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("totel",headNavIPage.getTotal());
        map.put("pages",headNavIPage.getPages());
        map.put("records",headNavIPage.getRecords());
        return new CommonResult(2000, "查询成功", map);
    }

    /**
     * 添加导航
     *
     * @param fuzzySearch
     * @return
     */
    @Override
    public CommonResult addNavigation(FuzzySearch fuzzySearch) {
        HeadNav headNav = new HeadNav();
        headNav.setSkipWay(fuzzySearch.getSkipWay());
        headNav.setIsDisable("1");
        headNav.setIsAdd(0);
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("head_id", fuzzySearch.getHeadId());
        int update = headNavMapper.update(headNav, wrapper);

        String head = redisTemplate.opsForValue().get("head");
        if(head!=null && !"".equals(head)){
            Boolean head1 = redisTemplate.delete("head");
        }

        return new CommonResult(2000, "添加成功", null);
    }

    /**
     * 添加导航回显     添加导航需要传 1
     *
     * @param
     * @return
     */
    @Override
    public CommonResult navigationQuery() {
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", "0");
        wrapper.eq("is_add", "1");
        List<HeadNav> navs = headNavMapper.selectList(wrapper);
        return new CommonResult(2000, "查询导航成功", navs);
    }

    /**
     * 修改导航状态
     *
     * @param
     * @return
     */
    @Override
    public CommonResult updateStatus(String headId,String isDisable) {
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("head_id", headId);
        HeadNav headNav = new HeadNav();
        headNav.setIsDisable(isDisable);
        int update = headNavMapper.update(headNav, wrapper);

        String head = redisTemplate.opsForValue().get("head");
        if(head!=null && !"".equals(head)){
            Boolean head1 = redisTemplate.delete("head");
        }
        return new CommonResult(2000, "修改状态成功", null);
    }

    /**
     * 删除导航
     *
     * @param headId
     * @return
     */
    @Override
    public CommonResult deleteNavigation(String headId) {
        int i = headNavMapper.deleteById(headId);

        String head = redisTemplate.opsForValue().get("head");
        if(head!=null && !"".equals(head)){
            Boolean head1 = redisTemplate.delete("head");
        }
        return new CommonResult(2000, "删除成功", null);
    }

    /**
     * 修改导航信息
     *
     * @param fuzzySearch
     * @return
     */
    @Override
    public CommonResult updateNavigationDetails(FuzzySearch fuzzySearch) {
        QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
        wrapper.eq("head_id", fuzzySearch.getHeadId());
        HeadNav headNav = new HeadNav();
        headNav.setHeadName(fuzzySearch.getHeadName());
        headNav.setSkipWay(fuzzySearch.getSkipWay());
        headNav.setSort(fuzzySearch.getSort());

        int update = headNavMapper.update(headNav, wrapper);

        String head = redisTemplate.opsForValue().get("head");
        if(head!=null && !"".equals(head)){
            Boolean head1 = redisTemplate.delete("head");
        }
        return new CommonResult(2000,"修改导航信息成功", null);
    }

    /**
     * 前端头部导航
     *
     * @return
     */
    @Override
    public CommonResult navigation() {

        if (redisTemplate.hasKey("head")) {
            //查看redis是否存在head
            List<HeadNav> list = JSON.parseObject(redisTemplate.opsForValue().get("head"), List.class);
            return new CommonResult(2000, "查询成功", list);

        } else {
            QueryWrapper<HeadNav> wrapper = new QueryWrapper<>();
            wrapper.eq("is_disable", 0);
            wrapper.eq("is_deleted", 0);
            wrapper.eq("is_add",0);
            wrapper.orderByAsc("sort");
            List<HeadNav> headNavs = headNavMapper.selectList(wrapper);
            String s = JSON.toJSONString(headNavs);
            //System.out.println(s);
            redisTemplate.opsForValue().set("head", s,24, TimeUnit.HOURS);

            return new CommonResult(2000, "查询头部导航成功", headNavs);
        }
    }


}
